﻿using System.IO;
using System.Xml;
using System.Reflection;
using CounterServerModel.Exceptions;

namespace CounterServerModel
{
    /// <summary>
    /// A class responsible for configuration.
    /// </summary>
    public class Configurator
    {
		private string m_ConfigFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "app.config");
        public void Configure(string i_SQLServerAddress, string i_DBName, string i_AppUserName, string i_AppPassword)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

			XmlWriter xmlWriter = XmlWriter.Create(m_ConfigFilePath, settings);

            xmlWriter.WriteStartElement("SmartCountersConfiguration");
            xmlWriter.WriteElementString("SQLServerAddress", i_SQLServerAddress);
            xmlWriter.WriteElementString("DBName", i_DBName);
            xmlWriter.WriteElementString("AppUserName", i_AppUserName);
            xmlWriter.WriteElementString("AppPassword", i_AppPassword);
            xmlWriter.WriteEndElement();
            
            xmlWriter.Close();
        }

        public string GetSQLServerAddress()
        {
			if (!File.Exists(m_ConfigFilePath))
            {
                throw new DBNotDeployedException();
            }
            
            XmlDocument doc = new XmlDocument();
			doc.Load(m_ConfigFilePath);

            XmlNodeList nodes = doc.GetElementsByTagName("SQLServerAddress");
            if (nodes.Count == 0)
            {
                throw new DBNotDeployedException();
            }

            return nodes[0].InnerText;
        }

        public string GetDBName()
        {
			if (!File.Exists(m_ConfigFilePath))
            {
                throw new DBNotDeployedException();
            }

            XmlDocument doc = new XmlDocument();
			doc.Load(m_ConfigFilePath);

            XmlNodeList nodes = doc.GetElementsByTagName("DBName");
            if (nodes.Count == 0)
            {
                throw new DBNotDeployedException();
            }

            return nodes[0].InnerText;
        }

        public string GetAppUserName()
        {
			if (!File.Exists(m_ConfigFilePath))
            {
                throw new DBNotDeployedException();
            }

            XmlDocument doc = new XmlDocument();
			doc.Load(m_ConfigFilePath);

            XmlNodeList nodes = doc.GetElementsByTagName("AppUserName");
            if (nodes.Count == 0)
            {
                throw new DBNotDeployedException();
            }

            return nodes[0].InnerText;
        }

        public string GetAppPassword()
        {
			if (!File.Exists(m_ConfigFilePath))
            {
                throw new DBNotDeployedException();
            }

            XmlDocument doc = new XmlDocument();
			doc.Load(m_ConfigFilePath);

            XmlNodeList nodes = doc.GetElementsByTagName("AppPassword");
            if (nodes.Count == 0)
            {
                throw new DBNotDeployedException();
            }

            return nodes[0].InnerText;
        }
    }
}
